﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UIT_CV_Management
{
    public partial class UserControlNgoaiNgu : UserControl
    {

        bool isEveryThingOK = false;

        bool isTenNgoaiNguOK = false;
        bool isNgheOK = false;
        bool isNoiOK = false;
        bool isDocOK = false;
        bool isVietOK = false;

        //object
        DanhGiaTrinhDoNgoaiNgu danhGiaTrinhDoNgoaiNgu;
        DaoTaoNgoaiNgu daoTaoNgoaiNgu;

        public UserControlNgoaiNgu()
        {
            InitializeComponent();
            // Hide all pictureBx
            this.hideAllErrorImage();

            // INIT language
            this.initLanguage();
        }
        private void hideAllErrorImage()
        {
            pictureBoxTenNgoaiNgu.Visible = false;
            pictureBoxTen.Visible = false;
            pictureBoxNghe.Visible = false;
            pictureBoxNoi.Visible = false;
            pictureBoxDoc.Visible = false;
            pictureBoxViet.Visible = false;
        }
        private void initLanguage()
        {
            lblTenNgoaiNgu.Text = Language._("NGOAINGU_FORM_TENNGOAINGU_LBL");
            lblDanhGiaTrinhDo.Text = Language._("NGOAINGU_FORM_DANHGIATRINHDO_LBL");
            lblNghe.Text = Language._("NGOAINGU_FORM_NGHE_LBL");
            lblNoi.Text = Language._("NGOAINGU_FORM_NOI_LBL");
            lblDoc.Text = Language._("NGOAINGU_FORM_DOC_LBL");
            lblViet.Text = Language._("NGOAINGU_FORM_VIET_LBL");
        }

        private void txbTenNgoaiNgu_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbTenNgoaiNgu.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isTenNgoaiNguOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbTenNgoaiNgu.BackColor = Color.Tomato;
                utpTip.Value = Language._("NGOAINGU_FORM_TIP_TENNGOAINGU_NULL");
                utpTip.Visible = true;
                pictureBoxTen.Visible = false;
            }
            else
            {
                isTenNgoaiNguOK = true;
                txbTenNgoaiNgu.BackColor = Color.White;
                pictureBoxTen.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void textBoxNghe_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;
            float outResult;

            // check Luong is numberic
            if (!float.TryParse(textBoxNghe.Text, out outResult))
            {
                isHasError = true;
            }
            else
                if ((outResult < 0) || (outResult > 10))
                {
                    isHasError = true;
                }

            if (isHasError)
            {
                isNgheOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                textBoxNghe.BackColor = Color.Tomato;
                utpTip.Value = Language._("NGOAINGU_FORM_TIP_NGHE_NULL");
                utpTip.Visible = true;
                pictureBoxNghe.Visible = false;
            }
            else
            {
                isNgheOK = true;
                textBoxNghe.BackColor = Color.White;
                pictureBoxNghe.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void textBoxNoi_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;
            float outResult;

            // check Luong is numberic
            if (!float.TryParse(textBoxNoi.Text, out outResult))
            {
                isHasError = true;
            }
            else
                if ((outResult < 0) || (outResult > 10))
                {
                    isHasError = true;
                }
            if (isHasError)
            {
                isNoiOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                textBoxNoi.BackColor = Color.Tomato;
                utpTip.Value = Language._("NGOAINGU_FORM_TIP_NOI_NULL");
                utpTip.Visible = true;
                pictureBoxNoi.Visible = false;
            }
            else
            {
                isNoiOK = true;
                textBoxNoi.BackColor = Color.White;
                pictureBoxNoi.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void textBoxDoc_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;
            float outResult;

            // check Luong is numberic
            if (!float.TryParse(textBoxDoc.Text, out outResult))
            {
                isHasError = true;
            }
            else
                if ((outResult < 0) || (outResult > 10))
                {
                    isHasError = true;
                }
            if (isHasError)
            {
                isDocOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                textBoxDoc.BackColor = Color.Tomato;
                utpTip.Value = Language._("NGOAINGU_FORM_TIP_DOC_NULL");
                utpTip.Visible = true;
                pictureBoxDoc.Visible = false;
            }
            else
            {
                isDocOK = true;
                textBoxDoc.BackColor = Color.White;
                pictureBoxDoc.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void textBoxViet_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;
            float outResult;

            // check Luong is numberic
            if (!float.TryParse(textBoxViet.Text, out outResult))
            {
                isHasError = true;
            }
            else
                if ((outResult < 0) || (outResult > 10))
                {
                    isHasError = true;
                }
            if (isHasError)
            {
                isVietOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                textBoxViet.BackColor = Color.Tomato;
                utpTip.Value = Language._("NGOAINGU_FORM_TIP_VIET_NULL");
                utpTip.Visible = true;
                pictureBoxViet.Visible = false;
            }
            else
            {
                isVietOK = true;
                textBoxViet.BackColor = Color.White;
                pictureBoxViet.Visible = true;
                utpTip.Visible = false;
            }
        }

        public bool isNgoaiNguOK()
        {
            //Validate
            txbTenNgoaiNgu_Validating(txbTenNgoaiNgu, null);
            textBoxNghe_Validating(textBoxNghe, null);
            textBoxNoi_Validating(textBoxNoi, null);
            textBoxDoc_Validating(textBoxDoc, null);
            textBoxViet_Validating(textBoxViet, null);

            isEveryThingOK = isTenNgoaiNguOK && isNgheOK && isNoiOK && isDocOK && isVietOK;

            return isEveryThingOK;
        }

        public DaoTaoNgoaiNgu getDaoTaoNgoaiNgu()
        {
            if (this.isNgoaiNguOK())
            {
                // init DanhGiaTrinhDo
                danhGiaTrinhDoNgoaiNgu = new DanhGiaTrinhDoNgoaiNgu(double.Parse(textBoxNghe.Text), double.Parse(textBoxNoi.Text)
                , double.Parse(textBoxDoc.Text), double.Parse(textBoxViet.Text));

                //init DaoTaoNgoaiNgu
                daoTaoNgoaiNgu = new DaoTaoNgoaiNgu(txbTenNgoaiNgu.Text,danhGiaTrinhDoNgoaiNgu);
            }
            return daoTaoNgoaiNgu;
        }

    }
}
